package com.thinkgem.jeesite.common.security.shiro.session;

import java.io.Serializable;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.web.servlet.Cookie;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;

public class SessionManager extends DefaultWebSessionManager {
    private static Log log = LogFactory.getLog(SessionManager.class);

    public SessionManager() {
        super();
    }

    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
        // 如果参数中包含“__sid”参数，则使用此sid会话。
        // 例如：http://localhost/project?__sid=xxx&__cookie=true
        // 其实这里还可以使用如下参数：cookie中的session名称：如：JSESSIONID=xxx,路径中的
        // ;JESSIONID=xxx，但建议还是使用 __sid参数。
       // String url = ((HttpServletRequest) request).getRequestURI();
        String state = request.getParameter("state") ;
        String body = ((HttpServletRequest)request).getParameter("body");
        String sid = null;
        if(body!=null){
            sid = getSessionStr(body);
        }
        log.warn("jeesite_session_body:"+body);
        
        String ssid = request.getParameter("__sid");
        
        if (StringUtils.isNotBlank(ssid)||StringUtils.isNotBlank(sid)||StringUtils.isNotBlank(state)) {
            // 是否将sid保存到cookie，浏览器模式下使用此参数。
            if(StringUtils.isNotBlank(ssid)){
                sid=ssid;
            }
            if(StringUtils.isNotBlank(state)){
                sid = state;
            }
             log.warn("jsid in URL" + sid);
            HttpServletRequest rq = (HttpServletRequest) request;
            HttpServletResponse rs = (HttpServletResponse) response;
            Cookie template = getSessionIdCookie();
            Cookie cookie = new SimpleCookie(template);
            cookie.setValue(sid);
            cookie.saveTo(rq, rs);
            // 设置当前session状态
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE,
                    ShiroHttpServletRequest.URL_SESSION_ID_SOURCE); // session来源与url
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, sid);
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);

            // System.out.println("finsh set cookie");
            return sid;
        } else {
            // System.out.println("normally getSessionId");
            return super.getSessionId(request, response);
        }
    }

    public String getSessionStr(String code){
        String sessionStr = null;
        if(StringUtils.isNotBlank(code)){
       
            String[] str = code.split("_");
            if(str.length>0){
                sessionStr= str[str.length-1];
            }
        }
        return sessionStr;
    }
}